package Test;

import javax.swing.*;
import java.awt.event.*;
import java.io.*;

 class FileMenuExample {

    private static final int BUFFER_SIZE = 4096;

    private File currentFile;

    public static void main(String[] args) {
        FileMenuExample editor = new FileMenuExample();
        editor.createAndShowGUI();
    }

    private void createAndShowGUI() {
        JFrame frame = new JFrame("菜单栏文件示例");
        frame.setSize(600, 400);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JTextArea textArea = new JTextArea(20, 40);

        JMenuBar menuBar = new JMenuBar();
        JMenu fileMenu = new JMenu("文件");

        // 创建菜单项
        JMenuItem newMenuItem = new JMenuItem("新建");
        JMenuItem openMenuItem = new JMenuItem("打开");
        JMenuItem saveMenuItem = new JMenuItem("保存");
        JMenuItem saveAsMenuItem = new JMenuItem("另存为");

        // 添加菜单项事件监听器
        newMenuItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                currentFile = null;
                textArea.setText("");
            }
        });

        openMenuItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JFileChooser fileChooser = new JFileChooser();
                int result = fileChooser.showOpenDialog(frame);
                if (result == JFileChooser.APPROVE_OPTION) {
                    currentFile = fileChooser.getSelectedFile();
                    try (FileReader fileReader = new FileReader(currentFile)) {
                        BufferedReader bufferedReader = new BufferedReader(fileReader);
                        StringBuilder sb = new StringBuilder();
                        String line;
                        while ((line = bufferedReader.readLine()) != null) {
                            sb.append(line).append("\n");
                        }
                        textArea.setText(sb.toString());
                    } catch (IOException ex) {
                        ex.printStackTrace();
                    }
                }
            }
        });

        saveMenuItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (currentFile != null) {
                    try (FileWriter fileWriter = new FileWriter(currentFile)) {
                        fileWriter.write(textArea.getText());
                    } catch (IOException ex) {
                        ex.printStackTrace();
                    }
                } else {
                    saveAsMenuItem.doClick();
                }
            }
        });

        saveAsMenuItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JFileChooser fileChooser = new JFileChooser();
                int result = fileChooser.showSaveDialog(frame);
                if (result == JFileChooser.APPROVE_OPTION) {
                    currentFile = fileChooser.getSelectedFile();
                    try (FileWriter fileWriter = new FileWriter(currentFile)) {
                        fileWriter.write(textArea.getText());
                    } catch (IOException ex) {
                        ex.printStackTrace();
                    }
                }
            }
        });

        // 添加菜单项到菜单
        fileMenu.add(newMenuItem);
        fileMenu.add(openMenuItem);
        fileMenu.add(saveMenuItem);
        fileMenu.add(saveAsMenuItem);

        // 添加菜单到菜单栏
        menuBar.add(fileMenu);

        // 添加菜单栏到窗口
        frame.setJMenuBar(menuBar);

        // 添加组件到面板
        frame.add(new JScrollPane(textArea));

        // 设置窗口可见
        frame.setVisible(true);
    }
}
